/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ServiceUpdateCampaignid{

    public void ServiceUpdateCampaignid () throws InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException{        
        Map<String, String> allCampaignid = new HashMap<>();
        MysqlDataManager cpc = new MysqlDataManager();
        //allCampaignid = cpm.getAllCampaign();
        Map<Integer, String> uv_uc = new HashMap<>();
        MysqlDataManagerMain mysqldatamanager = new MysqlDataManagerMain();        
        String currentDate = TimeUtil.getCurrentTimeInString();//2012-07-24
        //campaign theo date
        allCampaignid = cpc.getAllCampaignByDate(currentDate);
        //uv-uc theo campaign
        //uv_uc = mysqldatamanager.getUvUcByCampid(currentDate);
        //insert data
        StringBuilder sbff = new StringBuilder();
        Iterator it = allCampaignid.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();            
            //System.out.println(pairs.getKey() + " = " + pairs.getValue());
            String key = (String) pairs.getKey();
            String[] arrTemp = new String[4];
            arrTemp = key.split("_");
            String camp = arrTemp[0];
            String dt = arrTemp[1];            
            String dataValue = (String) pairs.getValue();
            String[] arr = new String[4];
            arr = dataValue.split("_");
            String totalview = arr[0];
            String totalclick = arr[1];
            String money = arr[2];            
            String uc ="";
            String uv ="";
                        
            if(uv_uc.containsKey(Integer.valueOf(camp)))
            {
                String uValue = (String) uv_uc.get(Integer.valueOf(camp));
                uc = uValue.split("_")[0];
                uv = uValue.split("_")[1];
            } else {
                uc = "0";
                uv = "0";
            }            
            //float ctr = 3.7f;
            //ctr = Float.valueOf(totalclick)/Float.valueOf(totalview);
            if(Integer.valueOf(totalview) > 0)
            {
                DecimalFormat df = new DecimalFormat("0.00#");
                String ctr = df.format(Float.valueOf(totalclick)/Float.valueOf(totalview)*100);
                String strResult = "('" + camp + "'," + totalclick + "," + totalview + "," +totalclick +"," +totalclick +"," + totalview +","+ Integer.valueOf(uv) + "," + Integer.valueOf(uc) +",'"+ dt + "'," + ctr + "," + 0 + "," + money + ")";
                sbff.append(strResult).append(",");
            }else{
               String ctr = "0.0";
               String strResult = "('" + camp + "'," + totalclick + "," + totalview + "," +totalclick +"," +totalclick +"," + totalview +","+ Integer.valueOf(uv) + "," + Integer.valueOf(uc) +",'"+ dt + "'," + ctr + "," + 0 + "," + money + ")";
                sbff.append(strResult).append(",");
            }
                    
        }
        
        String relStr = sbff.toString();
        String valInsert = relStr.substring(0, (relStr.length()-1));
        cpc.insertDataCampainTemp(valInsert);
        
    }
}
